package cn.com.dhcc.ddi.task.dao;

import cn.com.dhcc.app.core.dao.BaseDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.pub.CM;
import cn.com.dhcc.ddi.datasource.vo.TableInfoVo;
import cn.com.dhcc.ddi.datasource.vo.wapper.TableInfoRowWapper;
import cn.com.dhcc.ddi.task.vo.TaskTab;
import cn.com.dhcc.ddi.task.vo.wapper.TaskTabRowWapper;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @日期：2016/11/15 16:29
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
@Repository
public class TaskTabDao extends BaseDao {
    private static Logger logger = Log.getLogger(Log.LOGTYPE.DDI);

    /**
     * 获取任务交换表信息
     * @param taskId
     * @return
     * @throws DaoException
     */
    public List<TableInfoVo> getListBytaskId(String taskId) throws DaoException {
        String msg;
        String sql = "select t1.*,t2.where_condition from " + SI_TABLE("t_ddi_table_info") + "  t1 "
                +    "left join " + SI_TABLE("t_ddi_task_tab") + " t2 on t1.id = t2.table_id "
                +    "where t2.task_id = ? "
                +    "order by tab_order ";
        try {
            return CM.getDao().list("发送任务-发送表信息", sql,
                    new TableInfoRowWapper(),new Object[] {taskId});
        } catch (DaoException e) {
            msg = "发送任务-发送表信息失败";
            logger.error(msg, e);
            throw new DaoException(msg, e);
        }
    }

    /**
     * 获取任务发送表列表
     * @param  taskId 任务id
     * @return 任务发送表列表
     * @throws DaoException 
     */
    public List<TaskTab> getTaskTabByTaskId(String taskId) throws DaoException {
    	String msg;
        String sql = "SELECT t1.*, t2.table_name FROM " + SI_TABLE("t_ddi_task_tab") + "  t1 "
        		+    "LEFT JOIN " + SI_TABLE("t_ddi_table_info") + " t2 ON t1.table_id = t2.id "
                +    "WHERE t1.task_id = ? "
                +    "ORDER BY t1.tab_order ";
        try {
            return CM.getDao().list("获取任务发送表列表", sql, new TaskTabRowWapper(),new Object[] {taskId});
        } catch (DaoException e) {
            msg = "获取任务发送表列表失败";
            throw new DaoException(msg, e);
        }
    	
    }
    
    /**
	 * 根据任务id删除任务对应表
	 * @param  taskId 任务id
	 * @return 成功次数
	 * @throws DaoException
	 */
	public int deleteByTaskId(String taskId) throws DaoException{
		String msg;
		String sql = "DELETE FROM " + SI_TABLE("t_ddi_task_tab") + " WHERE task_id=?";
		try {
			return CM.getDao().delete("删除任务对应表", sql,new Object[] {taskId});
		} catch (DaoException e) {
			msg = "删除任务对应表失败";
			throw new DaoException(msg, e);
		}
	}
    
    /**
     * 获取交换表信息
     * @param id 主键
     * @return
     * @throws DaoException
     */
    public TaskTab get(String id) throws DaoException {
       return getDao().getModuleById("",TaskTab.class,new Object[]{id});
    }


    /**
     * 新增
     * @par task
     * @return
     * @throws DaoException
     */
    public int insert(TaskTab obj) throws DaoException {
        return getDao().insertModule("新增",obj);
    }

    /**
     * 修改
     * @param task
     * @return
     * @throws DaoException
     */
    public int update(TaskTab obj) throws DaoException {
        return getDao().updateModuleByIdExecuteNull("修改",obj);
    }

    /**
     * 新增或修改
     * @param task
     * @return
     * @throws DaoException
     */
    public int insertOrUpdate(TaskTab obj) throws DaoException {
        return getDao().saveOrUpdateModuleByIdExecuteNull("新增或修改",obj);
    }

    public List<TaskTab> list(String taskId) throws DaoException {
        String sql = "select * from " + SI_TABLE("t_ddi_task_tab") + " where task_id=?";
        return getDao().listModule("",sql,TaskTab.class,new Object[]{taskId});
    }

    /**
     * 获取任务交换表
     * @param taskId 任务id
     * @param tableId   表id
     * @return
     * @throws DaoException
     */
    public TaskTab getTaskTabByTaskIdAndTableId(String taskId, String tableId) throws DaoException {
        String sql = "select * from " + SI_TABLE("t_ddi_task_tab") + " where task_id=? and table_id=?";
        return getDao().getModule("",sql,TaskTab.class,new Object[]{taskId,tableId});
    }
}
